package com.nice.dao;

import com.alibaba.fastjson.JSON;
import com.nice.common.CacheUtils;
import com.nice.common.ReadObject;
import com.nice.entity.TradeSchema;
import org.springframework.stereotype.Repository;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 模拟一些DAO操作，实际是存到文本
 */
@Repository
public class ObjectDao {
    ReadObject readObject = new ReadObject();
    //自选牌价表
    public final static String TABLE_SELECT_INDEX_NAME = "TABLE_SELECT_INDEX_NAME";
    //交易表
    public final static String TABLE_TRADE_SCHEMA_INFO = "TABLE_TRADE_SCHEMA_INFO";

    //查询自选牌价
    public Map<String, String> selectIndexNameMap() {
        //优先查缓存
        Map<String, String> selectIndexNameMapFromCache = CacheUtils.selectIndexName;
        Map<String, String> selectIndexNameMapFromIo = null;
        //缓存为空，查文本数据库，获取序列化对象
        if (selectIndexNameMapFromCache == null || selectIndexNameMapFromCache.isEmpty()) {
            selectIndexNameMapFromIo = (Map<String, String>) readObject.readObject(TABLE_SELECT_INDEX_NAME);
        }
        //内存和文件都没有，返回一个初始化的map
        if ((selectIndexNameMapFromCache == null || selectIndexNameMapFromCache.isEmpty())&&(selectIndexNameMapFromIo == null || selectIndexNameMapFromIo.isEmpty())) {
            return new ConcurrentHashMap<String, String>();
        }
        if (selectIndexNameMapFromIo != null && !selectIndexNameMapFromIo.isEmpty()) {
            System.out.println("初始化缓存selectIndexName");
            CacheUtils.selectIndexName.putAll(selectIndexNameMapFromIo);
        }
        return CacheUtils.selectIndexName;
    }

    //插入自选牌价
    public void insertSelectIndexNameMap(String indexName) {
        System.out.println("插入自选对象：" + indexName);
        selectIndexNameMap();//如果缓存为空，查询下数据库，并将数据库值存入缓存
        CacheUtils.selectIndexName.put(indexName, indexName);
        readObject.writeObjcect(CacheUtils.selectIndexName, TABLE_SELECT_INDEX_NAME);
    }

    //删除，，哈哈，重置嘛
    public void deleteSelectIndexNameMapByIndexName(String indexName) {
        System.out.println("删除自选对象：" + indexName);
        selectIndexNameMap();//如果缓存为空，查询下数据库，并将数据库值存入缓存
        CacheUtils.selectIndexName.remove(indexName);
        readObject.writeObjcect(CacheUtils.selectIndexName, TABLE_SELECT_INDEX_NAME);
    }

    //查询交易信息
    public Map<String, TradeSchema> selectTradeSchemaInfo() {
        Map<String, TradeSchema> selectTradeSchemaInfoFromCache = CacheUtils.stradeSchemaInfo;
        Map<String, TradeSchema> selectTradeSchemaInfoFromIo = null;

        if (selectTradeSchemaInfoFromCache == null || selectTradeSchemaInfoFromCache.isEmpty()) {
            selectTradeSchemaInfoFromIo = (Map<String, TradeSchema>) readObject.readObject(TABLE_TRADE_SCHEMA_INFO);
        }
        //内存和文件都没有，返回一个初始化的map
        if ((selectTradeSchemaInfoFromCache == null || selectTradeSchemaInfoFromCache.isEmpty())&&(selectTradeSchemaInfoFromIo == null || selectTradeSchemaInfoFromIo.isEmpty())) {
            return new ConcurrentHashMap<String, TradeSchema>();
        }
        if (selectTradeSchemaInfoFromIo != null && !selectTradeSchemaInfoFromIo.isEmpty()) {
            System.out.println("初始化缓存selectTradeSchemaInfo");
            CacheUtils.stradeSchemaInfo.putAll(selectTradeSchemaInfoFromIo);
        }
        return CacheUtils.stradeSchemaInfo;
    }

    //插入自选牌价
    public void insertTradeSchemaInfo(TradeSchema tradeSchema) {
        System.out.println("插入新交易tradeSchema");
        selectTradeSchemaInfo();//避免内存未初始化
        CacheUtils.stradeSchemaInfo.put(tradeSchema.getSeqNo(), tradeSchema);
        readObject.writeObjcect(CacheUtils.stradeSchemaInfo, TABLE_TRADE_SCHEMA_INFO);
    }

    //删除，，哈哈，重置嘛
    public void deleteTradeSchemaInfo(TradeSchema tradeSchema) {
        System.out.println("删除交易对象=" + JSON.toJSONString(tradeSchema));
        selectTradeSchemaInfo();//避免内存未初始化
        CacheUtils.stradeSchemaInfo.remove(tradeSchema.getSeqNo());
        readObject.writeObjcect(CacheUtils.stradeSchemaInfo, TABLE_TRADE_SCHEMA_INFO);
    }
}
